package br.com.bbcalibracao.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionFactory {
		
		private static final String STR_DRIVER = "com.mysql.jdbc.Driver"; // "org.gjt.mm.mysql.Driver";  
	    private static final String DATABASE = "bbcalibracao7";  
	    private static final String IP = "186.202.152.17"; 
	    private static final String STR_CON = "jdbc:mysql://" + IP + ":3306/" + DATABASE;  
	    private static final String USER = "bbcalibracao7";  
	    private static final String PASSWORD = "bbbc561";  
		  
	    public static Connection getConexao() throws Exception {  
	        Connection con = null;  
	        try {  
	            Class.forName(STR_DRIVER);  
	            con = DriverManager.getConnection(STR_CON, USER, PASSWORD);  
	            return con;  
	        } catch (ClassNotFoundException e) {  
	        	e.printStackTrace();
	            String errorMsg = "Driver nao encontrado";  
	            throw new Exception(errorMsg, e);  
	        } catch (SQLException e) {  
	        	e.printStackTrace();
	            String errorMsg = "Erro ao obter a conexao";  
	            throw new Exception(errorMsg, e);  
	        }  
	    }  
		  
	    public static void closeAll(Connection con) {  
	        try {  
	            if (con != null) {  
	                con.close();  
	            }  
	        } catch (Exception e) {  
	            System.out.println("ERRO: Nao foi possivel fechar a conexao com o banco");  
	            e.printStackTrace();
	        }  
	    }  
	  
	    public static void closeAll(Connection con, Statement stmt, ResultSet rs) {  
	        try {  
	            if (con != null || stmt != null) {  
	                closeAll(con, stmt);  
	            }  
	            if (rs != null) {  
	                rs.close();  
	            }  
	        } catch (Exception e) {  
	        	System.out.println("Nao foi possivel fechar o resultSet do banco");  
	        	e.printStackTrace();
	        }  
	    }  
	  
	    public static void closeAll(Connection con, Statement stmt) {  
	        try {  
	            if (con != null) {  
	                closeAll(con);  
	            }  
	            if (stmt != null) {  
	                stmt.close();  
	            }  
	        } catch (Exception e) {  
	        	System.out.println("Nao foi possivel fechar a statement do banco");  
	        	e.printStackTrace();
	        }  
	    }  
	
}
